<?php
class member extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览会员', '1-2');
		$da = $this -> pub -> get_setting('会员', 'Menbers');
		$this -> load -> view('adminx/member', $da);
	}

	function get() {
		$this -> pub -> logp('获取会员信息', '1-2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;
		$this -> db -> where('subscribe_time >', $day1);
		$this -> db -> where('subscribe_time <', $day2);

		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');

			if (is_numeric($this -> input -> post('key'))) {
				if (strlen($this -> input -> post('key')) == 11) {
					$this -> db -> where('phone', $this -> input -> post('key'));
				} else {
					$this -> db -> where('mid', $this -> input -> post('key'));
				}
			} else {
				$this -> db -> like('nickname', $this -> input -> post('key'));
			}

			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			if ($this -> input -> post('openid')) {
				$this -> db -> where('openid', $this -> input -> post('openid'));
			}
			$q = $this -> db -> get_where('member');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			if (is_numeric($this -> input -> post('key'))) {
				if (strlen($this -> input -> post('key')) == 11) {
					$this -> db -> where('phone', $this -> input -> post('key'));
				} else {
					$this -> db -> where('mid', $this -> input -> post('key'));
				}
			} else {
				$this -> db -> like('nickname', $this -> input -> post('key'));
			}
		} else {
			if ($this -> input -> post('openid')) {
				$this -> db -> where('openid', $this -> input -> post('openid'));
			}
			if ($this -> input -> post('type') > 0) {
				$this -> db -> where('type', $this -> input -> post('type'));
			}
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('member');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('mid', 'desc');
		if ($this -> input -> post('type') > 0) {
			$this -> db -> where('type', $this -> input -> post('type'));
		}
		$this -> db -> where('subscribe_time >', $day1);
		$this -> db -> where('subscribe_time <', $day2);
		if ($this -> input -> post('openid')) {
			$this -> db -> where('openid', $this -> input -> post('openid'));
		}
		$q = $this -> db -> get_where('member');
		$da['content'] = '';
		$type[1] = '普通客户';
		$type[2] = '准微店店长';
		$type[3] = '微店店长';
		$type[4] = '禁言客户';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['mid'] . '</td>';
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img width="48" src="' . ($v['headimgurl'] ? $v['headimgurl'] : '/mex/lance/img/tx.jpg') . '" /></a></td>';
			$da['content'] .= '<td>' . ($v['nickname'] ? $v['nickname'] : '未关注') . '&nbsp;&nbsp;&nbsp; ' . ($v['sex'] == 1 ? '男' : '女') . '<br>' . $v['country'] . ',' . $v['province'] . ',' . $v['city'] . '<br>多客服：' . ($v['kf_account'] ? $v['kf_account'] : '未指派') . '</td>';
			$da['content'] .= '<td>' . $type[$v['type']] . '</td>';
			$da['content'] .= '<td>' . ($v['subscribe_time'] ? date('Y-m-d', $v['subscribe_time']) : '') . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['mid'] . '\')" type="button" class="btn default"><i class="fa fa-edit"></i>设置</button><button onclick="get_ewm(' . $v['mid'] . ',\'' . $v['nickname'] . '\',1)" type="button" class="btn default blue"><i class="fa fa-qrcode"></i>二维码</button><a href="/adminx/yl/?mid=' . $v['mid'] . '#/shop/bill" class="btn btn-primary"><i class="fa fa-cloud"></i>模拟登陆</a><button onclick="re_ewm(' . $v['mid'] . ')" type="button" class="btn default red"><i class="fa fa-trash-o"></i> 重置推广图片</button> <button onclick="get_tj(' . $v['mid'] . ',\'' . $v['nickname'] . '\')" type="button" class="btn green"><i class="icon-bar-chart"></i> 统计</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function get_member_ewm() {
		$this -> pub -> logp('获取会员二维码 - ' . $this -> input -> post('mid'), '1-2');
		$da['ewm'] = $this -> pub -> get_ewm($this -> input -> post('mid'), $this -> input -> post('type'));
		echo $da['ewm'];
	}

	function get_one() {
		$this -> pub -> logp('获取会员信息 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('member', array('mid' => $this -> input -> post('idx')));
		$da = $q -> row_array(0);

		$q = $this -> db -> get_where('member', array('mid' => $da['tj_mid']));
		$tj_m = $q -> row_array(0);
		$da['content'] = '<p><img width="120" src="' . $da['headimgurl'] . '" /><p/>';
		$da['content'] .= '<p>姓名：' . $da['name'] . '<p/>';
		$da['content'] .= '<p>电话：' . $da['phone'] . '<p/>';
		$da['content'] .= '<p>邮箱：' . $da['email'] . '<p/>';
		$da['content'] .= '<p>结算：' . ($da['js'] == 1 ? '支付宝' : '工行储蓄卡') . '<p/>';
		$da['content'] .= '<p>结算账户：' . $da['yh'] . '<p/>';
		$da['content'] .= '<p>住址：' . $da['zz'] . '<p/>';
		$da['content'] .= '<p>留言：' . $da['ly'] . '<p/>';
		$da['content'] .= '<p>大东家：' . $tj_m['name'] . '(' . $tj_m['nickname'] . ')' . '<p/>';
		$da['content'] .= '<p><img width="120" src="' . $tj_m['headimgurl'] . '" /><p/>';
		$da['typex'] = 1;

		$q = $this -> db -> get_where('mphone', array('mid' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$mphone = $q -> row_array(0);
			$da['mphone'] = $mphone['phone'];
			$da['yzm'] = $mphone['yzm'];
		} else {
			$da['mphone'] = '';
			$da['yzm'] = '';
		}

		echo json_encode($da);
	}

	function up() {
		$this -> pub -> logp('审核微店店长 - ' . $this -> input -> post('idx'), '1-2');
		$da['bz'] = $this -> input -> post('bz');
		$da['type'] = $this -> input -> post('type');
		$da['tj_mid'] = $this -> input -> post('tj_mid');
		$da['kf_account'] = $this -> input -> post('kf_account');
		$this -> db -> update('member', $da, array('mid' => $this -> input -> post('idx')));

		if ($this -> input -> post('kf_account')) {
			$this -> db -> select('openid,nickname');
			$this -> db -> where('mid', $this -> input -> post('idx'));
			$q = $this -> db -> get_where('member');
			$m = $q -> row_array(0);
			$this -> pub -> send_dkf_zd($m['openid'], $this -> input -> post('kf_account'));
		}
		//修改手机
		$pd = 1;
		if ($this -> input -> post('mphone')) {
			$q = $this -> db -> get_where('mphone', array('phone' => $this -> input -> post('mphone')));
			if ($q -> num_rows() > 0) {
				$m = $q -> row_array(0);
				if ($m['phone'] == $this -> input -> post('mphone')) {
					$this -> _up_phone($this -> input -> post('idx'), $this -> input -> post('mphone'), $this -> input -> post('yzm'));
				} else {
					$pd = 2;
				}
			} else {
				$this -> _up_phone($this -> input -> post('idx'), $this -> input -> post('mphone'), $this -> input -> post('yzm'));
			}
		}
		
		echo json_encode(array('type' => $pd));
	}

	function _up_phone($mid, $phone, $yzm) {
		$q = $this -> db -> get_where('mphone', array('mid' => $mid));
		if ($q -> num_rows() > 0) {
			$q = $this -> db -> update('mphone', array('phone' => $phone, 'yzm' => $yzm), array('mid' => $mid));
		} else {
			$in['mid'] = $mid;
			$in['phone'] = $phone;
			$in['yzm'] = $yzm;
			$in['yzm_d'] = $yzm;
			$in['type'] = 2;
			$in['mktime'] = mktime();
			$this -> db -> insert('mphone', $in);
		}

	}

	function info($mid) {
		$this -> pub -> logp('查看会员信息', '1-2-3');
		if ($this -> session -> userdata('state') == 3) {
			$mid = $this -> session -> userdata('midx');
		}
		$da = $this -> pub -> get_setting('会员信息', 'index');
		$q = $this -> db -> get_where('member', array('mid' => $mid));
		$da['m'] = $q -> row_array(0);

		//获取剩余
		$da['zj_sy'] = $this -> pub -> get_jb($mid);
		//总提现
		$this -> db -> where('mid', $mid);
		$this -> db -> where('type', -1);
		$this -> db -> where('state >', 0);
		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$da['zj_tx'] = $ls['sum(je)'];
		//总收益
		$this -> db -> where('mid', $mid);
		$this -> db -> where('type in(2,4)');
		$this -> db -> where('state >', 0);
		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$da['zj_zsy'] = $ls['sum(je)'];

		//获取他推荐的账单
		$this -> db -> order_by('id', 'desc');
		$q = $this -> db -> get_where('jb', array('mid' => $mid));
		$da['bill'] = $q -> result_array();

		//获取粉丝
		$this -> db -> select('mid,nickname,headimgurl,subscribe_time');
		$q = $this -> db -> get_where('member', array('tj_mid' => $mid));
		$da['fs'] = $q -> result_array();

		//获取3个级别的上级
		$da['ms'] = $this -> pub -> get_san($mid);

		$this -> load -> view('adminx/info', $da);
	}

	//修改个人密码
	function up_password() {
		$this -> pub -> logp('修改自己的密码 - ' . $this -> session -> userdata('sid'), '1-2-3');
		$da['type'] = 2;
		$da['msg'] = '';
		$q = $this -> db -> get_where('staff', array('staff_id' => $this -> session -> userdata('sid')));
		$m = $q -> row_array(0);
		if ($m['password'] == md5($this -> input -> post('password'))) {
			if ($this -> input -> post('password1') != $this -> input -> post('password2')) {
				$da['msg'] = '两次输入密码不对！';
			} else {
				$da['type'] = 1;
				$this -> db -> update('staff', array('password' => md5($this -> input -> post('password1'))), array('staff_id' => $this -> session -> userdata('sid')));
				$da['msg'] = '密码修改成功！';
			}
		} else {
			$da['msg'] = '原来的密码不对！';
		}
		echo json_encode($da);
	}

	//二维码的重新生成
	function re_ewm() {
		ignore_user_abort(true);
		if (!defined('DS')) {
			define('DS', DIRECTORY_SEPARATOR);
		}
		$this -> pub -> logp('重新生成二维码 - ' . $this -> input -> post('idx'), '1-2');
		@unlink(FCPATH . 'upfiles' . DS . 'member' . DS . $this -> config -> item('qrcode_pay') . DS . $this -> input -> post('idx') . '.png');
		$this -> pub -> haibao($this -> input -> post('idx'));
		echo json_encode(array('type' => 1));
	}

	function get_tj() {
		$this -> pub -> logp('重新生成二维码 - ' . $this -> input -> post('mid'), '1-2-3');

		$this -> db -> where('mid', $this -> input -> post('mid'));
		$this -> db -> where('mktime >', strtotime($this -> input -> post('time1')));
		$this -> db -> where('mktime <', 86400 + strtotime($this -> input -> post('time2')));
		$this -> db -> where('type >', 1);
		$this -> db -> where('state >', 0);
		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);

		$da['content'] = '结果：￥' . $ls['sum(je)'];
		$da['type'] = 1;
		echo json_encode($da);

	}

}
?>